#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct node
{
  char userid[50];
  char pwd[50];
}

main()
{
  FILE *fp;
  int i,j,d=0,h,cmp=100,cmp2=100,x=0,k,t=0,result=0,result2=0;
  char id[50];
  struct node *hash;
  struct node *array1[1000];
  struct node *array2[1000];
  struct node *array3[1000];
  
  char uid[30];
  char pswd[30];
  fp=fopen("names","r");
  for(i=0;i<150;i++)
  {
    d=0;
    hash=malloc(sizeof(struct node));
    fscanf(fp,"%s %s",hash->userid,hash->pwd);
    strcpy(id,hash->userid);
    for(k=0;id[k]!='\0';k++);
    
    for (j=0;j<k;j++)
      d=d-j+id[j];
    
    h=(d+k)%150;
    
    if (h<50)
    {
      while(array1[h]!='\0')
      {
	h=h+1;
	if(h==50)
	  h=0;
      }
      array1[h]=hash;
    }
    
    if (h>=50 && h<100)
    {
      while(array2[h]!='\0')
      {
	h=h+1;
	if(h==100)
	  h=50;
      }
      array2[h]=hash;
    }
    
    if (h>=100 && h<150)
    {
      while(array3[h]!='\0')
      {
	h=h+1;
	if(h==150)
	  h=100;
      }
      array1[h]=hash;
    }
  }
  
  while (t<3)
  {
    x=0;
    printf("Enter a username:");
    scanf("%s",uid);
    printf("Enter a password:");
    scanf("%s",pswd);
    
    for(k=0;uid[k]!='\0';k++);
    
    for(j=0;j<k;j++)
      x=x-j+uid[j];
    
    h=(x+k)%150;
    if (h<50){
      result = strncmp(array1[h]->userid, uid, cmp);
      result2 = strncmp(pswd, array1[h]->pwd, cmp2);
    }
    
    if (h>=50 && h<100){
      result = strncmp(array2[h]->userid, uid, cmp);
      result2 = strncmp(pswd, array2[h]->pwd, cmp2);
    }
    if (h>=100 && h<150){
      result = strncmp(array3[h]->userid, uid, cmp);
      result2 = strncmp(pswd, array3[h]->pwd, cmp2);
    }
    while(h<150)
    {
      if(result==0)
	
      {
	if(result2==0)
	{printf("Password matched. \n");
	break;
	t=4;
	}
	else{
	  printf("Incorrect password. \n");
	  break;}
      }
      else
	h=h+1;
    }
    if(h==150)
      printf("No such user id exists.\n");
    t=t+1;
  }
  if (t==3)
    printf("Too many tries!!!\n");
}

